package com.spring.invoice.controller;

import com.spring.invoice.service.Arith;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Controller
public class ReckonController {

    @RequestMapping(value="/index")
    @ResponseBody
    public ModelAndView goHome(){
        ModelAndView mode = new ModelAndView();
        //mode.addObject("name", "zhangsan");
        mode.setViewName("index");
        return mode;
    }

    @RequestMapping(method = RequestMethod.POST,value="/invoiceSplit/api")
    @ResponseBody
    public String invoiceSplit(HttpServletRequest request){
        try{
            List<String> list = new ArrayList<String>();
            double shuliang = Double.parseDouble(request.getParameter("shuliang"));
            double danjia = Double.parseDouble(request.getParameter("danjia"));
            double jine = Double.parseDouble(request.getParameter("jine"));
            double maxJE = 115999.99;
            //判断是否输入张数
            if(StringUtils.isEmpty(request.getParameter("zhangshu"))){
                if(jine <= maxJE){
                    return "{\"code\":1,\"msg\":\"总金额小于" + maxJE + "，无需拆分\"}";
                }
                double maxSL = 0.00;
                double xje = 0.00;//小票金额
                double xSL = 0.00;//小票数量
                while (jine >= maxJE){
                    if(xje == 0){
                        xSL = Arith.round(Arith.div(Arith.div(maxJE,1.16),danjia),4);
                        xje = Arith.mul(xSL,1.16,danjia);
                        while (xje > maxJE){
                            xSL = Arith.sub(xSL,0.0001);
                            xje = Arith.mul(xSL,1.16,danjia);
                        }
                        do{
                            int last = Double.toString(xje).indexOf(".");
                            int xsws = Double.toString(xje).substring(last + 1).length();//小数位数
                            char a = '0';
                            char b = '0';
                            char c = '0';
                            if(xsws >= 3)
                                a = Double.toString(xje).charAt(last + 3);
                            if(xsws >= 4)
                                b = Double.toString(xje).charAt(last + 4);
                            if(xsws >= 5)
                                c = Double.toString(xje).charAt(last + 5);
                            if(a == '0' && b == '0' && c == '0'){
                                break;
                            }
                            xSL = Arith.sub(xSL,0.0001);
                            xje = Arith.mul(xSL,1.16,danjia);
                        } while (true);
                    }
                    jine = Arith.sub(jine,xje);
                    maxSL = Arith.add(maxSL,xSL);
                    list.add(resultReqJson(xSL,danjia,Double.toString(xje)));
                }
                if(jine > 0){
                    double xSL1 = Arith.sub(shuliang,maxSL); //这里采取总数量-（115999.99计算出来的数量）
                    list.add(resultReqJson(xSL1,danjia,Double.toString(jine)));
                }
                return list.toString();
            } else {
                int zhangshu = Integer.parseInt(request.getParameter("zhangshu"));
                if(zhangshu <= 1){
                    return "{\"code\":1,\"msg\":\"拆分张数必须大于1张。\"}";
                }
                int shuliangInt = (int)(shuliang * 100);
                int shuliangYS = shuliangInt % zhangshu;
                int xSL = ((shuliangInt - shuliangYS) / (zhangshu)) / 100;
                double xJE = Arith.round(Arith.mul(xSL,danjia,1.16),2);
                if(xJE > maxJE){
                    return "{\"code\":1,\"msg\":\"拆分后金额不能大于" + maxJE + "元，请重新拆分！\"}";
                }
                double xJEt = 0.00;
                int xSLl = 0;
                for(int i = 1;i < zhangshu;i++){
                    list.add(resultReqJson(xSL,danjia,Double.toString(xJE)));
                    xJEt = Arith.add(xJEt,xJE);
                    xSLl = xSLl + xSL;
                }
                double bg_xSL = Arith.sub(shuliang,xSLl);
                double xJEl = Arith.sub(jine,xJEt);
                xJEl = Arith.round(xJEl,2);
                if(xJEl > maxJE){
                    return "{\"code\":1,\"msg\":\"拆分后金额不能大于" + maxJE + "元，请重新拆分！\"}";
                }
                list.add(resultReqJson(bg_xSL,danjia,Double.toString(xJEl)));
                return list.toString();
            }

        } catch (Exception e){
            e.printStackTrace();
            return "{\"code\":1,\"msg\":\""+e.getMessage()+"\"}";
        }
    }

    private String resultReqJson(double shuliang,double danjia,String jine){
        StringBuffer sb = new StringBuffer("{\"shuliang\": " + shuliang + ",\"danjia\": " + danjia + ",\"shuilv\": \"16%\",\"jine\": " + jine + "}");
        return sb.toString();
    }
}
